<?php
namespace app\api\controller\assay;
use think\File;
use think\facade\Env;
use app\common\model\user\User as UserModel;
use app\common\model\company\Company;
use app\common\model\orderdata\Goods;
use app\common\model\quotation\QuotationList;
use app\common\model\rebate\RebateList;
use app\common\model\rebate\Rebate as RebateModel;
use app\api\controller\Base;
use app\api\controller\Baseuser;
use app\common\model\revenue_ap\Raplist as RapListModel;
use app\common\model\revenue_ap\Rap as RapModel;
use app\common\model\assay\Assaylist;
use app\common\model\assay\Assay as AssayModel;

//化验管理 送样
class Assaysub extends Baseuser
{
	//初始化
    public function initialize(){
    	parent::initialize();
		// Rolepower::powerguard(101004);
	}
	
	//送样登记
	//撤销送样信息
	//修改送样登记信息
	//获取送样列表
	//获取送样详细信息

	/**
	 * 送样登记
	 * @return [type] [description]
	 */
	public function create(){
		//创建档案
		$data = input('post.');
		$id = input('CompanyId');//企业id
		$cdata = Company::get($id);
		if(empty($cdata)){
			return myerr('没有找到此企业信息');
		}
		$data['CompanyId'] = $id;
		//创建登记表
		$id = Assaylist::user_create($data);
		return myok($id);
	}

	//撤销送样信息
	public function deleteassay(){
		$id = input('id');
		if(empty($id)){
			return myerr('缺少参数，请重新尝试');
		}
		$ndata['id'] = $id;
		$ndata['IsDelete'] = 1;
		$data = Assaylist::user_reinfo($ndata);
		return myok('删除成功');
	} 

	/**
	 * 修改送审查信息
	 * @return [type] [description]
	 */
	public function reinfo(){
		$id = input('AssayListId');
		$idata = input('post.');
		$wtnote = true;//是否记录日志信息
		if(empty($id)){
			return myerr('缺少参数，请重新尝试');
		}
		$sb = Assaylist::get($id);
		if(empty($sb)){
			return myerr('没找到此数据');
		}
		if(!empty($sb['SubUserId'])){
			if($sb['SubUserId']!== $this->user_id){
				return myerr('没权限修改此数据');
			}
		}else{
			$wtnote = false;
		}
		//将接收人修改为此用户
		$rdata = ['id'=>$id,'SubUserId'=>$this->user_id];
		if($sb['State'] < Assaylist::STATEING){
			$rdata['State'] = Assaylist::STATEING;
		}
		$changearr = ['SubTime','SubImg','SubNote'];
		foreach ($changearr as $key => $value) {
			if(isset($idata[$value])){
				$rdata[$value] = $idata[$value];
			}
		}
		//修改记录表信息
		$res = Assaylist::user_reinfo($rdata,$wtnote,false);
		//修改字表记录数据
		$rlist = json_decode($idata['goodinfo'],true);
		foreach ($rlist as $key => $value) {
			$nrdata = ['id'=>$value['AssayId'],'SubImg'=>$value['SubImg'],'SubNote'=>$value['AccNote'],'GoodName'=>$value['GoodName'],'Price'=>$value['Price']];
			AssayModel::user_reinfo($nrdata,$wtnote,false);
		}
		return myok('修改成功');
	}


	/**
	 * 获取送样详细信息 getreinfo
	 * @return [type] [description]
	 */
	public function getinfo(){
		$id = input('id');
		if(empty($id)){
			return myerr('缺少参数，请重新尝试');
		}
		$ndata['id'] = $id;
		$ndata['IsDelete'] = 1;
		$data = Assaylist::user_reinfo($ndata);
		return myok($data);
	}

	public function getlist(){
		$data = input('get.');
        $data['Type'] = 'Assaysub';
        $data['order'] = 'SubTime desc';
        $data = init_where($data);
        if (Rolepower::ispower(204007)) {
            //管理员权限
            //直接通过无需筛选
        }else if (Rolepower::ispower(201003)) {
            //业务员
            $data['where'][] = ['CreateUserId',$this->user_id];
        }else  if (Rolepower::ispower(101006)) {
            //企业负责人
            $company = Company::user_getinfo();
            $data['where'][] = ['CompanyId',$company['CompanyId']];
        } else {
            return moerr('权限不足，无法查看信息','goback');
        }
        $list = CompanyTouchmodel::usergetlist($data);
        return myok($list); 
	}

}
